//
//  AppDelegate.swift
//  Weibo24
//
//  Created by apple on 16/2/24.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

/*
    比较大的项目--如果超过30个页面,建议大家用纯代码或者xib
*/

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //睡眠2秒钟

        // 创建数据库,为什么在这里创建?
        // 让程序一开始启动的时候,就创建好数据库的访问
        CZDBManager.shareInstance.openDB()
        
        
        //添加window,设置根控制器
        window = UIWindow(frame: kUIScreenBounds)
        
        window?.makeKeyAndVisible()
        
        // 问题: 如果用户没有登录,应该显示欢迎界面?
        //  没有登录显示访客视图
        
        //每次登陆用户,都需要显示欢迎界面
        if CZUserViewModel.shareInstance.isLogin! {
            
            window?.rootViewController = CZWelcomeViewController()
            
        } else {
            window?.rootViewController = CZTabBarViewController()
        }
        
        
        
        // 程序刚刚创建的时候,就设置好NavBar和 TabBar的渲染
        setupAppreace()
        
        //添加一个通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "changeRootVC", name:kNotificaionChangeRootVC, object: nil)
        
        //
        return true
        
    }

    // 通知 在这里改变根控制器
    func changeRootVC(){
    
        window?.rootViewController = CZTabBarViewController()
        
    }
    
    private func setupAppreace(){
        //如果不改变tintColor的颜色的画,系统默认渲染蓝色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
    }
    
    deinit{
        
        NSNotificationCenter.defaultCenter().removeObserver(self, name: kNotificaionChangeRootVC, object: nil)
        
    }
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

